/**
 * @(#)SourceTreeVisitor.java, 2008-4-30.
 * Copyright 2008  Program Analysis Research Group(PARG),Institute of Software, Peking University.
 * All rights reserved.
 */
package edu.pku.sei.pgie.analyzer.core;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;

/**
 * @author HeLi
 */
public abstract class SourceTreeVisitor {
    List<ISourceTreeVisitorListener> visitListeners;

    public SourceTreeVisitor() {
        visitListeners = new ArrayList<ISourceTreeVisitorListener>();
    }

    public void fireJavaElement(IJavaElement javaElement) {
        Iterator iterator = visitListeners.iterator();
        while (iterator.hasNext()) {
            ISourceTreeVisitorListener listener = (ISourceTreeVisitorListener) iterator
                    .next();
            listener.onVisitSource(javaElement);
        }
    }

    public void registerListener(ISourceTreeVisitorListener listener) {
        if (listener != null) {
            visitListeners.add(listener);
        }
    }

    public void removeListener(ISourceTreeVisitorListener listener) {
        if (listener != null) {
            visitListeners.remove(listener);
        }
    }

    public boolean visit(ICompilationUnit compilationUnit) {
        return true;
    }

    public boolean visit(IPackageFragment pckageFragment) {
        return true;
    }

    public boolean visit(IPackageFragmentRoot fragmentRoot) {
        return true;
    }
}
